using System.Collections.Generic;
using BookingSystem.NHibernate;
using NHibernate;
using NHibernate.Criterion;

namespace BookingSystem.Repository
{
    public class RepositoryBase<T>
    {
        public RepositoryBase()
        {
            Session = SessionManager.CurrentSession();
        }

        public ISession Session { get; private set; }

        public T GetById(int id)
        {
            return Session.Get<T>(id);
        }

        public IList<T> GetAll()
        {
            return Session.CreateCriteria(typeof(T)).List<T>();
        }

        public void Persist(T obj)
        {
            Session.SaveOrUpdate(obj);
            Session.Flush();
        }

        public void Merge(T obj)
        {
            Session.Merge(obj);
        }

        public void Delete(T obj)
        {
            Session.Delete(obj);
            Session.Flush();
        }

        public IList<T> GetListExecutableCriteria(DetachedCriteria criteria)
        {
            return criteria.GetExecutableCriteria(Session).List<T>();
        }

        public T GetExecutableCriteria(DetachedCriteria criteria)
        {
            return criteria.GetExecutableCriteria(Session).UniqueResult<T>();
        }
    }
}